﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Xml.Serialization;

namespace NursingFormSystem
{
    /// <summary>
    /// 体温单测量数据
    /// </summary>
    [System.Runtime.InteropServices.ComVisible(false)]
    public class Measure
    {
        /// <summary>
        /// 初始化对象
        /// </summary>
        public Measure()
        {
        }

        private string _RecordID = null;
        /// <summary>
        /// 记录编号
        /// </summary>
        [DefaultValue(null)]
        public string RecordID
        {
            get { return _RecordID; }
            set { _RecordID = value; }
        }

        private DateTime _ValueDate = TemperatureDocument.NullDate;
        /// <summary>
        /// 测量日期
        /// </summary>
        [DefaultValue(typeof(DateTime), TemperatureDocument.NullDateString)]
        [XmlElement("ValueDate")]
        public DateTime ValueDate
        {
            get
            {
                return _ValueDate.Date ; 
            }
            set
            {
                _ValueDate = value.Date ; 
            }
        }

        private string _ValueTime = "0";
        /// <summary>
        /// 测量时刻，从0到23,支持到分钟
        /// </summary>
        [DefaultValue( "0" )]
        [XmlElement("ValueTime")]
        public string ValueTime
        {
            get
            {
                return _ValueTime; 
            }
            set
            {
                int tempTime = int.Parse(value.Split(':')[0].ToString()) % 24;
                if (tempTime < 0)
                {
                    tempTime = tempTime + 24;
                }
                _ValueTime = tempTime + ":" + (value.Split(':').Length >= 2 ? value.Split(':')[1].ToString() : "00");
                
            }
        }

        private string _ValueDateHour = null;
        /// <summary>
        /// 测量时间 HH:mm
        /// </summary>
        [XmlElement("ValueDateHour")]
        public string ValueDateHour
        {
            get
            {
                return _ValueDateHour;
            }
            set
            {
                _ValueDateHour = value;
            }
        }

        private string _MemoIn = null;
        /// <summary>
        /// 上事件
        /// </summary>
        [DefaultValue( null )]
        [XmlElement("MemoIn")]
        public string MemoIn
        {
            get { return _MemoIn; }
            set { _MemoIn = value; }
        }

        private string _MemoUp = null;
        /// <summary>
        /// 下事件
        /// </summary>
        [DefaultValue(null)]
        [XmlElement("MemoUp")]
        public string MemoUp
        {
            get { return _MemoUp; }
            set { _MemoUp = value; }
        }

        private string _Body_Temperature = null;
        /// <summary>
        /// 体温
        /// </summary>
        [DefaultValue(null)]
        [XmlElement("Body_Temperature")]
        public string Body_Temperature
        {
            get { return _Body_Temperature; }
            set { _Body_Temperature = value; }
        }
        /// <summary>
        /// 物理降温
        /// </summary>
        private string _FT = null;
        [DefaultValue(null)]
        [XmlElement("FT")]
        public string FT
        {
            get { return _FT; }
            set { _FT = value; }
        }

        private string _RT = null;
        /// <summary>
        /// 复试体温
        /// </summary>
        [DefaultValue(null)]
        [XmlElement("RT")]
        public string RT
        {
            get { return _RT; }
            set { _RT = value; }
        }

        private string _Pluse = null;
        /// <summary>
        /// 脉搏
        /// </summary>
        [DefaultValue(null)]
        [XmlElement("Pluse")]
        public string Pluse
        {
            get { return _Pluse; }
            set { _Pluse = value; }
        }

        private string _HeartRate = null;
        /// <summary>
        /// 心率
        /// </summary>
        [DefaultValue(null)]
        [XmlElement("HeartRate")]
        public string HeartRate
        {
            get { return _HeartRate; }
            set { _HeartRate = value; }
        }

        private string _Breath = null;
        /// <summary>
        /// 呼吸
        /// </summary>
        [DefaultValue(null)]
        [XmlElement("Breath")]
        public string Breath
        {
            get { return _Breath; }
            set { _Breath = value; }
        }
        private string _PainScore = null;
        /// <summary>
        /// 疼痛评分
        /// </summary>
        [DefaultValue(null)]
        [XmlElement("PainScore")]
        public string PainScore
        {
            get { return _PainScore; }
            set { _PainScore = value; }
        }

        private string _Line = null;
        /// <summary>
        /// 是否连线 1-连线
        /// </summary>
        [DefaultValue(null)]
        [XmlElement("Line")]
        public string Line
        {
            get { return _Line; }
            set { _Line = value; }
        }

        private string _DropedBeat = null;
        /// <summary>
        /// 脉搏是否短绌 1-是
        /// </summary>
        [DefaultValue(null)]
        [XmlElement("DropedBeat")]
        public string DropedBeat
        {
            get { return _DropedBeat; }
            set { _DropedBeat = value; }
        }

        private string _Respirator = null;
        /// <summary>
        /// 呼吸是否用呼吸机 1-是
        /// </summary>
        [DefaultValue(null)]
        [XmlElement("Respirator")]
        public string Respirator
        {
            get { return _Respirator; }
            set { _Respirator = value; }
        }

        //private string _PacedRate = null;
        ///// <summary>
        ///// 是否起搏心率 1-是
        ///// </summary>
        //[DefaultValue(null)]
        //public string PacedRate
        //{
        //    get { return _PacedRate; }
        //    set { _PacedRate = value; }
        //}

        private DataRowState _RecordState = DataRowState.Unchanged;
        /// <summary>
        /// 记录状态
        /// </summary>
        [Browsable(false)]
        [System.Xml.Serialization.XmlIgnore]
        public DataRowState RecordState
        {
            get { return _RecordState; }
            set { _RecordState = value; }
        }

        /// <summary>
        /// 设置默认值
        /// </summary>
        public void SetDefaultValue()
        {
            this._Body_Temperature = "";
            this._Breath = "";
            this._FT = "";
            this._Pluse = "";
            this._PainScore ="";
            this._HeartRate = "";
            this._Line = "";
            this._DropedBeat = "";
            this._Respirator = "";
            //this._PacedRate = "";
        }

        /// <summary>
        /// 复制对象
        /// </summary>
        /// <returns>复制品</returns>
        public Measure Clone()
        {
            return (Measure)this.MemberwiseClone();
        }

    }

    /// <summary>
    /// 数据记录列表
    /// </summary>
    /// <remarks></remarks>
    [System.Runtime.InteropServices.ComVisible(false)]
    public class MeasureList : List<Measure>
    {
        /// <summary>
        /// 初始化对象
        /// </summary>
        public MeasureList()
        {
        }
        /// <summary>
        /// 深度复制
        /// </summary>
        /// <returns>复制品</returns>
        public MeasureList CloneDeeply()
        {
            MeasureList list = new MeasureList();
            foreach (Measure m in this)
            {
                list.Add(m.Clone());
            }
            return list;
        }

    }

    //internal class MeasureBindingList : BindingList<Measure>
    //{
    //    private MeasureList _ParentList = null;

    //    public MeasureList ParentList
    //    {
    //        get { return _ParentList; }
    //        set { _ParentList = value; }
    //    }
    //    protected override void RemoveItem(int index)
    //    {
    //        base.RemoveItem(index);
    //    }
    //    private bool _DataModified = false;

    //    public bool DataModified
    //    {
    //        get { return _DataModified; }
    //        set { _DataModified = value; }
    //    }

    //    private DateTime _CurrentDate = DateTime.Now;

    //    public DateTime CurrentDate
    //    {
    //        get { return _CurrentDate; }
    //        set { _CurrentDate = value; }
    //    }

    //    protected override void OnListChanged(ListChangedEventArgs e)
    //    {
    //        base.OnListChanged(e);
    //        if (e.ListChangedType == ListChangedType.ItemAdded)
    //        {
    //            Measure record = this[e.NewIndex];
    //            record.RecordState = DataRowState.Added;
    //            record.Val_Date = this.CurrentDate;
    //            record.SetDefaultValue();
    //        }
    //        else if (e.ListChangedType == ListChangedType.ItemChanged)
    //        {
    //            this[e.NewIndex].RecordState = DataRowState.Modified;
    //        }
    //        this.DataModified = true;
    //    }
    //}

    internal class MeasureComparer : IComparer<Measure>
    {
        public int Compare(Measure x, Measure y)
        {
            int result = DateTime.Compare(x.ValueDate, y.ValueDate);
            if (result == 0)
            {
                DateTime dt1 = Convert.ToDateTime(x.ValueTime.Contains(":") ? x.ValueTime : (x.ValueTime + ":00"));
                DateTime dt2 = Convert.ToDateTime(y.ValueTime.Contains(":") ? y.ValueTime : (y.ValueTime + ":00"));
                result = DateTime.Compare(dt1, dt2); ;
            }
            return result;
        }
    }
}
